package com.lizemin.mybatis.util;

import com.lizemin.mybatis.config.DataSourceProperties;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

public class YamlConfigReader {

    /**
     * 从 YAML 文件中读取以 power.datasource 为前缀的配置并填充到 DataSourceProperties 实例中
     * @param filePath YAML 文件的路径
     * @return DataSourceProperties 实例
     * @throws FileNotFoundException 如果文件未找到
     */
    public static DataSourceProperties loadDataSourceProperties(String filePath) throws FileNotFoundException {
        Yaml yaml = new Yaml();
        InputStream inputStream = new FileInputStream(filePath);
        Map<String, Object> yamlMap = yaml.load(inputStream);

        DataSourceProperties properties = new DataSourceProperties();

        // 获取 power 节点
        Map<String, Object> powerMap = (Map<String, Object>) yamlMap.get("power");
        if (powerMap != null) {
            // 获取 power.datasource 节点
            Map<String, Object> dataSourceMap = (Map<String, Object>) powerMap.get("datasource");
            if (dataSourceMap != null) {
                if (dataSourceMap.get("hostname") != null) {
                    properties.setHostname(dataSourceMap.get("hostname").toString());
                }
                if (dataSourceMap.get("port") != null) {
                    properties.setPort(dataSourceMap.get("port").toString());
                }
                if (dataSourceMap.get("database-name") != null) {
                    properties.setDatabaseName(dataSourceMap.get("database-name").toString());
                }
                if (dataSourceMap.get("user") != null) {
                    properties.setUser(dataSourceMap.get("user").toString());
                }
                if (dataSourceMap.get("password") != null) {
                    properties.setPassword(dataSourceMap.get("password").toString());
                }
            }
        }
        return properties;
    }

    /**
     * 将 YAML 文件内容转换为 Properties 对象
     * @param filePath YAML 文件的路径
     * @return 包含 YAML 配置的 Properties 对象
     */
    public static Properties convertYamlToProperties(String filePath) throws FileNotFoundException {
        Yaml yaml = new Yaml();
        InputStream inputStream = new FileInputStream(filePath);
        Map<String, Object> yamlMap = yaml.load(inputStream);

        Properties properties = new Properties();
        if (yamlMap != null) {
            flattenMap(yamlMap, properties, "");
        }
        return properties;
    }

    /**
     * 递归扁平化嵌套的 Map 并将键值对添加到 Properties 对象中
     * @param map 嵌套的 Map
     * @param properties Properties 对象
     * @param parentKey 父键，用于构建扁平键
     */
    private static void flattenMap(Map<String, Object> map, Properties properties, String parentKey) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = parentKey.isEmpty() ? entry.getKey() : parentKey + "." + entry.getKey();
            Object value = entry.getValue();
            if (value instanceof Map) {
                // 如果值是 Map，继续递归处理
                flattenMap((Map<String, Object>) value, properties, key);
            } else {
                // 如果值不是 Map，直接添加到 Properties 对象中
                properties.setProperty(key, String.valueOf(value));
            }
        }
    }

    public static void main(String[] args) {
        try {
            String path = "C:\\Users\\15871\\Documents\\java\\2025\\csdn\\power-mybatis\\JingDong-Mall\\src\\main\\resources\\application.yml";
            // 替换为实际的 YAML 文件路径
            Properties properties = convertYamlToProperties(path);
            properties.forEach((key, value) -> System.out.println(key + " = " + value));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}